package org.net.operation;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.info.server.Event;
import com.info.server.StartServer;
import com.info.utils.Byte2Hex;
import com.info.utils.InfoPacket;

/**
 * @author HAHA
 * 
 */
public class SendThread extends Thread {
	private static Logger logger=Logger.getLogger(SendThread.class);
	private Charset charset = Charset.forName(StartServer.gate_code);
	private CharsetEncoder encoder = this.charset.newEncoder();

	private SocketChannel channel = null;
	private ByteBuffer bbuf = null;
	private int sendBufSize = 8192;
	private int interval = 5;
	private Vector msglist = new Vector();
	private Event msg = new Event();
	private byte[] bytes = null;

	private int msgsize = 0;
	private int sendsize = 0;

	public SendThread() {
		this.interval = StartServer.SEND_INTERVAL;
		this.sendBufSize = StartServer.SEND_BUF;
		this.bbuf = ByteBuffer.allocateDirect(this.sendBufSize);
	}

	public synchronized void addMsg(Object obj) {
		try {
			this.msglist.add(obj);
		} catch (Exception x) {
			x.printStackTrace();
		}
	}

	public void run() {
		while (true)
			try {
				Thread.sleep(this.interval);
				this.bbuf.clear();
				if (this.msglist.size() > 0) {
					this.msg = (Event) this.msglist.remove(0);
					logger.info("SHW#反馈————"+this.msg.getMsgInfo()+"!");
					this.bytes = Byte2Hex.HexString2Bytes(this.msg.getMsgInfo());//this.msg.getMsgInfo().getBytes();
					this.msgsize = this.bytes.length;
					InfoPacket pack = this.msg.getUppack();
					if (pack != null) {
						String id = this.msg.getId();
						if ((id == null) || (id.equals(""))
								|| (id.length() != 12))
							id = "000000000000";
						this.channel = pack.getChannel();
						this.sendsize = 0;
						if (this.channel != null) {
							this.bbuf = this.bbuf.put(this.bytes,
									this.sendsize, this.msgsize);

							this.bbuf.flip();

							synchronized (this.channel) {
								if ((this.channel != null)
										&& (this.bbuf != null)) {
									this.sendsize = this.channel
											.write(this.bbuf);
									if (this.sendsize == 0)
									if (this.sendsize < this.msgsize) {
										this.msgsize -= this.sendsize;
									}
									if (this.sendsize == this.msgsize) {
										this.sendsize = 0;
									}
									if (this.sendsize > this.msgsize) {
										this.sendsize = 0;
									}
								} else {
									this.sendsize = 0;
									if (StartServer.logFlag) {
										System.out
												.println("SHW#发送信息的时候发现channel通道为空，此channel跳出循环");
									}
								}
							}
						}

					} else {
						this.sendsize = 0;
						this.msgsize = 0;
					}
					this.msg = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.info("SHW#下发的信息对象有错误:" + this.msg);
			}
	}

	/*@Override
	protected void executeEvent(Event paramEvent) {
		
	}*/
}